<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :lg="24" :xs="24">
        <div class="search-container">
          <el-form
            ref="queryFormRef"
            :model="queryParams"
            :inline="true"
            class="pull-left"
          >
            <el-form-item prop="keywords">
              <el-input
                v-model="queryParams.keywords"
                placeholder="分类名称"
                clearable
                style="width: 200px"
                @keyup.enter="handleQuery"
              />
            </el-form-item>
<!--            <el-form-item prop="shop_title">
              <el-input
                v-model="queryParams.shop_title"
                placeholder="所属店铺名称"
                clearable
                style="width: 200px"
                @keyup.enter="handleQuery"
              />
            </el-form-item>-->

            <el-form-item>
              <el-button type="primary" @click="handleQuery">
                <i-ep-search/>
                搜索
              </el-button>
              <el-button @click="resetQuery">
                <i-ep-refresh/>
                重置
              </el-button>
            </el-form-item>
          </el-form>
          <el-button
            v-hasPerm="['article:category:add']"
            type="success"
            @click="openDialog()"
            class="pull-right"
          >
            <i-ep-plus/>
            新增
          </el-button>
          <div class="clear"></div>
        </div>

        <el-card shadow="never" class="table-container">
          <el-table v-loading="loading" :data="lists">
            <el-table-column label="编号" align="center" prop="id" width="60"/>
            <el-table-column label="分类名称" align="center" prop="title"/>
<!--            <el-table-column label="所属店铺" align="center" prop="shop_title"/>-->
            <el-table-column label="状态" align="center" width="160">
              <template #default="scope">
                <el-tag :type="scope.row.status == 1 ? 'success' : 'info'">
                  {{ statusOptions[scope.row.status] }}
                </el-tag>
              </template>
            </el-table-column>
            <el-table-column label="创建时间" align="center" prop="create_time"/>
            <el-table-column label="操作" fixed="right" width="160">
              <template #default="scope">
                <el-button v-hasPerm="['article:category:edit']" type="primary" link size="small"
                           @click="openDialog(scope.row.id)">
                  <i-ep-edit/>
                  编辑
                </el-button>
                <el-button v-hasPerm="['article:category:delete']" type="danger" link size="small"
                           @click="handleDelete(scope.row.id)">
                  <i-ep-delete/>
                  删除
                </el-button>
              </template>
            </el-table-column>
          </el-table>

          <pagination
            v-if="total > 0"
            v-model:total="total"
            v-model:page="queryParams.pageNum"
            v-model:limit="queryParams.pageSize"
            @pagination="handleQuery"
          />
        </el-card>
      </el-col>
    </el-row>

    <editCategory
      :visible="editVisible"
      :categoryId="currentId"
      width="960px"
      @close="closeDialog"
    />

  </div>
</template>

<script setup lang="ts">
import {CategoryDetail, categoryList, CategoryQuery, deleteCategory} from "@/api/article/category";
import {statusOpts} from "@/utils/status";

defineOptions({
  name: "ArticleCategory",
  inheritAttrs: false,
});

const queryFormRef = ref(ElForm); // 查询表单
const categoryFormRef = ref(ElForm); // 表单
const statusOptions = statusOpts();

const loading = ref(false); //  加载状态
const queryParams = reactive<CategoryQuery>({
  pageNum: 1,
  pageSize: 10,
});

// 用户表单数据
const lists = ref<CategoryDetail[]>();
const total = ref(0); // 数据总数
const editVisible = ref(false);
const currentId = ref<number | undefined>();


/** 查询 */
function handleQuery() {
  loading.value = true;
  categoryList(queryParams)
    .then(({data}) => {
      lists.value = data.items;
      total.value = data.total;
    })
    .finally(() => {
      loading.value = false;
    });
}

/** 重置查询 */
function resetQuery() {
  queryFormRef.value.resetFields();
  queryParams.pageNum = 1;
  handleQuery();
}

function handleDelete(id?: number) {
  if (!id) {
    ElMessage.warning("请选择删除项");
    return;
  }
  ElMessageBox.confirm("确认删除已选中的数据项?", "温馨提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(() => {
    deleteCategory(id).then(() => {
      ElMessage.success("删除成功");
      // resetQuery();
      handleQuery();
    });
  });
}

/**
 * 打开弹窗
 *
 * @param type 弹窗类型  用户表单：user-form | 用户导入：user-import
 * @param id 用户ID
 */
async function openDialog(id?: number) {
  editVisible.value = true;
  console.log(editVisible)

  if (id)
    currentId.value = id;
}

/**
 * 关闭
 */
async function closeDialog(type?: any) {
  editVisible.value = false;
  currentId.value = undefined;

  if (type == 1) {
    handleQuery();
  }
}

onMounted(() => {
  handleQuery();
});

</script>
